package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB
var err error

func Init() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:c258f478e9ad1784478c25a556e45e6a" +
		"@tcp(124.220.135.42:3306)/warehouse?charset" +
		"=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		println("数据库连接失败")
	} else {
		fmt.Println("数据库连接成功")
	}

	err = DB.AutoMigrate(&Warehouse{})
	if err != nil {
		println("数据迁移失败")
	} else {
		fmt.Println("数据迁移成功")
	}

}
